package com.lvmama.rhino.job;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONArray;
import com.lvmama.rhino.pojo.search.FlowSearchStation;
import com.lvmama.rhino.service.StationService;
import com.lvmama.rhino.utils.HttpsUtil;

/** 
* @ClassName: StationDataJob 
* @Description: 定时拉取站点信息
* @author shenlinli
* @date 2017年4月27日 下午5:06:31  
*/
public class StationDataJob extends BaseQuartzJobBean  {
	@Autowired
    private StationService stationService;
	private Logger logger = Logger.getLogger(StationDataJob.class);

	@Override
	 public void invoke(JobExecutionContext context) {
      
		String jsons = HttpsUtil.requestPostForm("http://m.lvmama.com/bullet/index.php?s=/Api/getStations");
		String value = JSONObject.fromObject(jsons).get("datas").toString();
		logger.info("站点信息："+value);
		if(StringUtils.isNotEmpty(value)){
			JSONArray json = JSONArray.parseArray(value);
			if(json.size()>0){
				List<FlowSearchStation> stationList = new ArrayList<FlowSearchStation>();
				for(int i=0;i<json.size();i++){
					com.alibaba.fastjson.JSONObject object = json.getJSONObject(i);  
					String name = object.get("station_name").toString();
					int station_id = Integer.parseInt(object.getString("id"));
					String station_code = object.get("station_code").toString();
					FlowSearchStation station  =  new FlowSearchStation();
					station.setStationId(station_id);
					station.setStationName(name);
					station.setStationCode(station_code);
					stationList.add(station);
				}
				boolean flag = stationService.insertStationList(stationList);
				if(!flag){
					logger.info("批量插入站点信息有误");
				}
			}
		}
	}
	
}
